package Form;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import Control.CommonControls;
import Control.Customer_Controller;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.DefaultComboBoxModel;
import java.awt.GridLayout;
import javax.swing.JScrollPane;

public class Customer_CreateCustomerMembership extends JPanel {
	private JTextField name;
	private JTextField NRIC_Num;
	private JTextField Contact_Num;
	private JTextField Email;
	private JTextField UserName;
	private JPasswordField Password;
	private JPasswordField Cfm_Password;
	private JTextField SecurityQn;
	private JComboBox DOB_Month = new JComboBox();
	private JComboBox DOB_Year = new JComboBox();
	private JComboBox DOB_Day = new JComboBox();
	private JTextArea Residential_Add = new JTextArea();
	private JRadioButton BN_Female = new JRadioButton("Female");
	private JRadioButton BN_Male = new JRadioButton("Male");
	private JTextField SecurityAns = new JTextField();

	//Object Attributes 
	private JFrame parser;
	private JFrame frame;
	private Customer_Controller control;
	private JLabel lblRace;
	private JComboBox race;
	private JPanel pn_ind;
	private JPanel pn_corp;
	private JPanel pn_shared;
	private JButton bn_ind;
	private JButton bn_corp;
	private JTextField CName;
	private JLabel lblCompanyRegisteredNumber;
	private JTextField CRegNum;
	private JTextField CContactNum;
	private JLabel lblContactNumber;
	private JLabel lblCompanyAdd;
	private JLabel lblCompanyEmail;
	private JTextArea CAdd;
	private JTextField CEmail;
	private JButton btnForPresentation;
	
	public JFrame getParser() {
		return parser;
	}
	
	public void setParser(JFrame parser){
		this.parser = parser;
	}	
	
	public Customer_Controller getControl(){
		return control;
	}

	public void setControl (Customer_Controller control) {
		this.control = control; 
	}
	
	public Customer_CreateCustomerMembership(JFrame parser){
		this.setParser(parser);
		control = new Customer_Controller();
	
		setLayout(null);
		BN_Male.setSelected(true);
		BN_Male.setBounds(97, 95, 67, 23);
		getPn_ind().add(BN_Male);
		
		BN_Male.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BN_Female.setSelected(false);
			}
		});
		BN_Female.setBounds(165, 95, 67, 23);
		getPn_ind().add(BN_Female);
		
		BN_Female.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BN_Male.setSelected(false);
			}
		});
		DOB_Day.setBounds(107, 197, 46, 20);
		getPn_ind().add(DOB_Day);
		DOB_Day.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
		DOB_Month.setBounds(165, 197, 67, 20);
		getPn_ind().add(DOB_Month);
		DOB_Month.setModel(new DefaultComboBoxModel(new String[] {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}));
		DOB_Year.setBounds(257, 197, 81, 20);
		getPn_ind().add(DOB_Year);
		DOB_Year.setModel(new DefaultComboBoxModel(new String[] {"1995", "1994", "1993" , "1992" , "1991",
				"1990", "1989" , "1988" , "1987" , "1986" , "1985" ,"1984" , "1983", "1982" , "1981",
				"1980" , "1979" , "1978" , "1977" , "1976" , "1975" , "1974" , "1973" , "1972" , "1971",
				"1970" , "1969" , "1968" , "1967" , "1966" , "1965" , "1964", "1963" , "1962" , "1961",
				"1960" , "1959" , "1958" , "1957" , "1956" , "1955" , "1954" , "1953"
		}));
		
		
		
		JButton btnCreateMembership = new JButton("CREATE MEMBERSHIP");
		btnCreateMembership.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean valid = false; 
				valid = validateForm();
				if(valid == true){
					if(bn_ind.isEnabled()!=true){
						control.createMembership(getNRIC_Num().getText(),getname().getText(),getUserName().getText(),getEmail().getText(),Integer.parseInt(getContact_Num().getText())
								,getDOBDate(),Residential_Add.getText(),getGender(),SecurityQn.getText(),SecurityAns.getText(), race.getSelectedItem().toString(),"active"
								,(new String(getPassword().getPassword())),getTodayDate(),null,null,"INDIVIDUAL");
					} else if(bn_corp.isEnabled()!=true){
						control.createMembership(getCRegNum().getText(),getCName().getText(),getUserName().getText(),getCEmail().getText(),Integer.parseInt(getCContactNum().getText())
								,CommonControls.getCurrentDate(), getCAdd().getText(), (char) 0, SecurityQn.getText(),SecurityAns.getText(),"Z","active"
								,(new String(getPassword().getPassword())),getTodayDate(),null,null,"CORPORATE");
					}
					JOptionPane.showMessageDialog(frame, "You have successfully created an account!");
					//setVisible(false);
				}
			}
		});
				
			/*	System.out.println("start print : ");
				System.out.println("nric : "+getNRIC_Num().getText());
				System.out.println(getname().getText());
				System.out.println(getUserName().getText());
				System.out.println(Integer.parseInt(getContact_Num().getText()));
				System.out.println(getEmail().getText());
				System.out.println(getDOBDate());
				System.out.println(Residential_Add.getText());
				
				System.out.println(getGender());
				System.out.println(SecurityQn.getText());
				System.out.println(SecurityAns.getText());
				System.out.println( race.getSelectedItem().toString());
				System.out.println(getPassword().getPassword().toString());
				System.out.println(getTodayDate());*/
		add(getPn_ind());
		btnCreateMembership.setBounds(10, 579, 233, 23);
		add(btnCreateMembership);
		add(getBn_ind());
		add(getBn_corp());
		
		SecurityAns.setBounds(133, 134, 191, 20);
		SecurityAns.setColumns(10);
		
		pn_shared = new JPanel();
		pn_shared.setBounds(27, 388, 379, 180);
		add(pn_shared);
		pn_shared.setLayout(null);
		pn_shared.setVisible(true);
		
		JLabel lblUserId = new JLabel("User ID");
		lblUserId.setBounds(10, 8, 46, 14);
		pn_shared.add(lblUserId);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(10, 33, 91, 14);
		pn_shared.add(lblPassword);
		
		JLabel lblConfirmPassword = new JLabel("Confirm Password");
		lblConfirmPassword.setBounds(10, 58, 113, 14);
		pn_shared.add(lblConfirmPassword);
		
		JLabel lblSecurityQuestion = new JLabel("Security Question");
		lblSecurityQuestion.setBounds(10, 109, 113, 14);
		pn_shared.add(lblSecurityQuestion);
		
		JLabel lblSecurityAnswer = new JLabel("Security Answer");
		lblSecurityAnswer.setBounds(10, 134, 113, 14);
		pn_shared.add(lblSecurityAnswer);
		
		UserName = new JTextField();
		UserName.setBounds(133, 8, 191, 20);
		pn_shared.add(UserName);
		UserName.setColumns(10);
		
		Password = new JPasswordField();
		Password.setBounds(133, 33, 191, 19);
		pn_shared.add(Password);
		
		Cfm_Password = new JPasswordField();
		Cfm_Password.setBounds(133, 58, 191, 20);
		pn_shared.add(Cfm_Password);
		Cfm_Password.setColumns(10);
		
		SecurityQn = new JTextField();
		SecurityQn.setBounds(133, 109, 191, 20);
		pn_shared.add(SecurityQn);
		SecurityQn.setColumns(10);
		pn_shared.add(SecurityAns);
		
		pn_corp = new JPanel();
		pn_corp.setBounds(10, 109, 394, 221);
		add(pn_corp);
		pn_corp.setLayout(null);
		
		JLabel lblCompanyName = new JLabel("Company Name");
		lblCompanyName.setBounds(21, 24, 171, 14);
		pn_corp.add(lblCompanyName);
		pn_corp.add(getCName());
		pn_corp.add(getLblCompanyRegisteredNumber());
		pn_corp.add(getCRegNum());
		pn_corp.add(getCContactNum());
		pn_corp.add(getLblContactNumber());
		pn_corp.add(getLblCompanyAdd());
		pn_corp.add(getLblCompanyEmail());
		pn_corp.add(getCEmail());
		pn_corp.setVisible(false);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(202, 132, 182, 24);
		pn_corp.add(scrollPane);
		scrollPane.setViewportView(getCAdd());
		add(getBtnForPresentation());
	}

	//<------------------------------------- START OF GETTER --------------------------------------------------------> //

	public JPanel getPn_corp() {
		return pn_corp;
	}

	public void setPn_corp(JPanel pn_corp) {
		this.pn_corp = pn_corp;
	}

	public JTextField getname() {
		if(name == null){ 
			name = new JTextField();
		}
		return name;
	}


	public JTextField getNRIC_Num() {
		if( NRIC_Num== null){ 
			NRIC_Num = new JTextField();
		}
		return NRIC_Num;
	}


	public JTextField getContact_Num() {
		if( Contact_Num== null){ 
			Contact_Num = new JTextField();
		}
		return Contact_Num;
	}


	public JTextField getEmail() {
		if( Email == null){ 
			Email = new JTextField();
		}
		return Email;
	}

	public Date getDOBDate(){
		
		int month,day,year = 0;
		day = Integer.parseInt(DOB_Day.getSelectedItem().toString());
		month =  DOB_Month.getSelectedIndex()+1;
		year =  Integer.parseInt(DOB_Year.getSelectedItem().toString());
		String dateString = day+" "+month+" "+year;
		System.out.println("Date : "+ dateString);
		
		Calendar myCal = Calendar.getInstance();
		myCal.set(Calendar.YEAR, year);
		myCal.set(Calendar.MONTH, month);
		myCal.set(Calendar.DAY_OF_MONTH, day);
		Date date = myCal.getTime();
		
		return date;		
	}
	
	public String getTodayDate(){
		Date date = Calendar.getInstance().getTime();
		DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
		String reportDate = df.format(date);
		return reportDate;
	}
	
	public char getGender(){
		char gender = 'n';
		if(BN_Male.isSelected()==true){
			gender = 'M';
		}
		else if(BN_Female.isSelected()==true){
			gender = 'F';
		}
		return gender;
	}

	public JTextField getUserName() {
		if( UserName== null){ 
			UserName = new JTextField();
		}
		return UserName;
	}


	public JPasswordField getPassword() {
		if( Password== null){ 
			Password = new JPasswordField();
		}
		return Password;
	}

	public JTextField getSecurityQn() {
		if( SecurityQn == null){ 
			SecurityQn = new JTextField();
		}
		return SecurityQn;
	}


	public JTextField getSecurityAns() {
		if( SecurityAns== null){ 
			SecurityAns = new JTextField();
		}
		return SecurityAns;
	}
	private JLabel getLblRace() {
		if (lblRace == null) {
			lblRace = new JLabel("Race");
			lblRace.setBounds(10, 134, 46, 14);
		}
		return lblRace;
	}
	private JComboBox getRace() {
		if (race == null) {
			race = new JComboBox();
			race.setBounds(97, 131, 119, 20);
			race.setModel(new DefaultComboBoxModel(new String[] {"Chinese", "Malay", "Indian", "Others"}));
		}
		return race;
	}
	private JPanel getPn_ind() {
		if (pn_ind == null) {
			pn_ind = new JPanel();
			pn_ind.setBounds(34, 74, 379, 302);
			pn_ind.setLayout(null);
		
			JLabel lblName = new JLabel("NAME");
			lblName.setBounds(10, 36, 46, 14);
			pn_ind.add(lblName);
			
			name = new JTextField();
			name.setBounds(97, 33, 166, 20);
			pn_ind.add(name);
			name.setColumns(10);
			
			NRIC_Num = new JTextField();
			NRIC_Num.setBounds(97, 64, 166, 20);
			pn_ind.add(NRIC_Num);
			NRIC_Num.setColumns(10);
			
			JLabel lblNric = new JLabel("NRIC ");
			lblNric.setBounds(10, 75, 46, 14);
			pn_ind.add(lblNric);
			
			JLabel lblGender = new JLabel("Gender");
			lblGender.setBounds(10, 104, 46, 14);
			pn_ind.add(lblGender);
			pn_ind.add(getLblRace());
			pn_ind.add(getRace());
			
			JLabel lblDateOfBirth = new JLabel("Date Of Birth");
			lblDateOfBirth.setBounds(10, 200, 67, 14);
			pn_ind.add(lblDateOfBirth);
			
			Contact_Num = new JTextField();
			Contact_Num.setBounds(130, 228, 205, 20);
			pn_ind.add(Contact_Num);
			Contact_Num.setColumns(10);
			
			JLabel lblContactNumber = new JLabel("Contact Number");
			lblContactNumber.setBounds(10, 231, 99, 14);
			pn_ind.add(lblContactNumber);
			
			JLabel lblResidentialAddress = new JLabel("Residential Address");
			lblResidentialAddress.setBounds(10, 264, 143, 14);
			pn_ind.add(lblResidentialAddress);
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(130, 259, 205, 30);
			pn_ind.add(scrollPane);
			scrollPane.setViewportView(Residential_Add);
			
			JLabel lblEmailAddres = new JLabel("Email Addres");
			lblEmailAddres.setBounds(10, 161, 99, 14);
			pn_ind.add(lblEmailAddres);
			
			Email = new JTextField();
			Email.setBounds(97, 162, 210, 20);
			pn_ind.add(Email);
			Email.setColumns(10);
		}
		return pn_ind;
	}
	
	private JButton getBn_ind() {
		if (bn_ind == null) {
			bn_ind = new JButton("Individual");
			bn_ind.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					resetForms();
					pn_ind.setVisible(true);
					pn_corp.setVisible(false);
					//After complete then set to false. 
					//pn_shared.setVisible(true);
					bn_corp.setEnabled(true);
					bn_ind.setEnabled(false);
					
				}
			});
			bn_ind.setBounds(71, 19, 122, 23);
			bn_ind.setEnabled(false);		
		}
		return bn_ind;
	}
	private JButton getBn_corp() {
		if (bn_corp == null) {
			bn_corp = new JButton("Corporate");
			bn_corp.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					resetForms();
					pn_ind.setVisible(false);
					pn_corp.setVisible(true);
					pn_shared.setVisible(true);
					bn_corp.setEnabled(false);
					bn_ind.setEnabled(true);
				}
			});
			bn_corp.setBounds(205, 19, 122, 23);
		}
		return bn_corp;
	}
	
	private void resetForms(){
		name.setText("");
		NRIC_Num.setText("");
		Contact_Num.setText("");
		Email.setText("");
		UserName.setText("");
		Password.setText("");
		Cfm_Password.setText("");
		SecurityQn.setText("");
		SecurityAns.setText("");
		Residential_Add.setText("");
		CAdd.setText("");
		//CEmail.setText("");
		CName.setText("");
		CRegNum.setText("");
		CContactNum.setText("");
		BN_Female.setSelected(false);
		BN_Male.setSelected(false);
		
		//Birthday yet to set

		return;
	}
	
	//checking
	/*
	 *********************Method 1****************************
	  //SET everything into form
		String iName = (name.getText());
		String iNric = (NRIC_Num.getText());
		String iTel =(Contact_Num.getText());
		String iEmail=(Email.getText());

		String cAdd=(CAdd.getText());
		String cEmail=(CEmail.getText());
		String cName=(CName.getText());
		String cTel=(CContactNum.getText());
		String cRegNo=(CRegNum.getText());
		
		//TAKENOTE :: Race, gender... 
		
		String Username=(UserName.getText());
		String pwd= new String(Password.getPassword());
		String CfmPwd= new String (Cfm_Password.getPassword());
		String SeqQn =(SecurityQn.getText());
		String SeqAns=(SecurityAns.getText());
		
	
	if(iName.equals("") || iNric.equals("") || iTel.equals("") || iEmail.equals("")){}
	}
	<------------------------METHOD 2------------------------>
	boolean phoneValid = true;
	if(CommonControls.validatePanelFields(getPn_ind()) && phoneValid){
	
	else
	{
		if(!phoneValid){
			//METHOD// 
		}
	}*/
	
	private boolean validateForm(){
		boolean valid = false;
		if(validatePanelFields(getPn_shared())){
			valid = true;
		}
		if(!bn_ind.isEnabled()){
			valid = (validatePanelFields(getPn_ind()));
			
		}
		else if(!bn_corp.isEnabled()){
			valid = (validatePanelFields(getPn_corp()));
			
		}
		if(!valid){
			this.displayMessage(false, "Error(s) found in some fields.\nPlease check through fields highlighted in red before trying again.");
		}
		return valid;
	}
	
	public JPanel getPn_shared() {
		return pn_shared;
	}

	public void setPn_shared(JPanel pn_shared) {
		this.pn_shared = pn_shared;
	}

	private JTextField getCName() {
		if (CName == null) {
			CName = new JTextField();
			CName.setBounds(202, 24, 182, 20);
			CName.setColumns(10);
		}
		return CName;
	}
	private JLabel getLblCompanyRegisteredNumber() {
		if (lblCompanyRegisteredNumber == null) {
			lblCompanyRegisteredNumber = new JLabel("Company registered number");
			lblCompanyRegisteredNumber.setBounds(21, 62, 171, 14);
		}
		return lblCompanyRegisteredNumber;
	}
	private JTextField getCRegNum() {
		if (CRegNum == null) {
			CRegNum = new JTextField();
			CRegNum.setBounds(202, 62, 182, 20);
			CRegNum.setColumns(10);
		}
		return CRegNum;
	}
	private JTextField getCContactNum() {
		if (CContactNum == null) {
			CContactNum = new JTextField();
			CContactNum.setBounds(202, 101, 182, 20);
			CContactNum.setColumns(10);
		}
		return CContactNum;
	}
	private JLabel getLblContactNumber() {
		if (lblContactNumber == null) {
			lblContactNumber = new JLabel("Contact number ");
			lblContactNumber.setBounds(21, 101, 128, 14);
		}
		return lblContactNumber;
	}
	private JLabel getLblCompanyAdd() {
		if (lblCompanyAdd == null) {
			lblCompanyAdd = new JLabel("Company Address");
			lblCompanyAdd.setBounds(21, 139, 128, 14);
		}
		return lblCompanyAdd;
	}
	private JLabel getLblCompanyEmail() {
		if (lblCompanyEmail == null) {
			lblCompanyEmail = new JLabel("Company Email");
			lblCompanyEmail.setBounds(21, 175, 144, 14);
		}
		return lblCompanyEmail;
	}
	private JTextArea getCAdd() {
		if (CAdd == null) {
			CAdd = new JTextArea();
		}
		return CAdd;
	}
	
	public JTextField getCEmail() {
		if (CEmail == null) {
			CEmail = new JTextField();
			CEmail.setText("");
			CEmail.setBounds(202, 175, 182, 20);
		}
		return CEmail;
	}

	public void setCEmail(JTextField cEmail) {
		CEmail = cEmail;
	}

	public static boolean validatePanelFields(JPanel panel){
		boolean valid = true;
		
		Component[] components = panel.getComponents();
		for(int i = 0; i < components.length; i++){
			if(components[i] instanceof JTextField){
				JTextField field = (JTextField)components[i];
				if(field.getText().isEmpty() || field.getText().replace(" ", "").equals("") || field.getText() == null){
					valid = false;
					field.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
				}
				else{
					field.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
				}
			}
			if(components[i] instanceof JScrollPane){
				JScrollPane scrollPane = (JScrollPane)components[i];
				if(scrollPane.getViewport().getComponents()[0] instanceof JTextArea){
					JTextArea field = (JTextArea)scrollPane.getViewport().getComponents()[0];
					if(field.getText().isEmpty() || field.getText().replace(" ", "").equals("") || field.getText() == null){
						valid = false;
						scrollPane.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
					}
					else{
						scrollPane.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
					}
				}
			}
		}
		return valid;
	}

	public void displayMessage(boolean valid, String text){
		if(valid){
			JOptionPane.showMessageDialog(this, text, "System Message", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Toolkit.getDefaultToolkit().createImage("Resources/Images/Icons/tick.png").getScaledInstance(30, 30, Image.SCALE_DEFAULT)));
		}
		else{
			JOptionPane.showMessageDialog(this, text, "System Message", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Toolkit.getDefaultToolkit().createImage("Resources/Images/Icons/cross.png").getScaledInstance(30, 30, Image.SCALE_DEFAULT)));
		}
	}
	public JButton getBtnForPresentation() {
		if (btnForPresentation == null) {
			btnForPresentation = new JButton("For presentation");
			btnForPresentation.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					name.setText("Sam Kee");
					NRIC_Num.setText("S9209431H");
					Contact_Num.setText("81530589");
					Email.setText("SamGrim@mail.com");
					UserName.setText("SamK");
					SecurityQn.setText("Your full name");
					SecurityAns.setText("Sam Kee");
					Residential_Add.setText("SameGrim@mail");
					BN_Male.setSelected(true);

				}
			});
			btnForPresentation.setBounds(300, 579, 160, 23);
		}
		return btnForPresentation;
	}
}
